<?php
	defined('_JEXEC') or die('Restricted access');
?>
<script type="text/javascript">
	
	jQuery(document).ready(function(){
	   jQuery("#fil_redemp_go").click(function(){
			if(jQuery('#yfrom').val() !='' && jQuery('#yfrom').val() != '0'  && jQuery('#yfrom').val() != '0000' && jQuery('#yfrom').val() != 0){ 
				var yfrom  = parseInt(jQuery('#yfrom').val()) ;
			}else{
				var yfrom = '0000' ;
			}
			var mfrom = parseInt(jQuery('#mfrom').val()) ;
			var dfrom = parseInt(jQuery('#dfrom').val()) ;
		    if(jQuery('#yto').val() != '' && jQuery('#yto').val() != '0' && jQuery('#yto').val() != '0000' && jQuery('#yto').val() != 0 ){
			    var yto  = parseInt(jQuery('#yto').val()) ;
		    }else{
		    	<?php 
					$cur_year =  explode('-', date('Y-m-d')) ;
				?>
				var yto  = '<?php echo ($cur_year[0] + 1) ; ?>' ;
			}
		    var mto = parseInt(jQuery('#mto').val());
		    var dto  = parseInt(jQuery('#dto').val());
		    var from_day = new Date(yfrom, mfrom, dfrom);
		    var to_day = new Date(yto, mto, dto);
		    if(to_day < from_day){
		    	alert('"From date or To date is not correct."') ;
				return false ;
		   	}else{
	        	jQuery("#redemptions").text('Loading...').load('<?php print JRoute::_('index.php?option=com_wrd&controller=profile&layout=fil_redemp&tmpl=component',false); ?>',{'uid':<?php echo empty($this->data->user_id)? 0:$this->data->user_id ;?>,'yfrom':jQuery('#yfrom').val(), 'mfrom':jQuery('#mfrom').val(), 'dfrom':jQuery('#dfrom').val(), 'yto':jQuery('#yto').val(), 'mto':jQuery('#mto').val(), 'dto':jQuery('#dto').val()});
		   	}
	    });
	});
</script>	
<div class="row-fluid">
	<div class="alert">
	 	<table>
	 		<tr>
	 			<td valign='top'>
		 			 <table>
		 			 	<tr>
			 			 	<td>
				 			 	<?php if(!empty($this->data->member_id)):?>
									<span style='width:150px; padding-right: 30px;'>Member ID assigned</span><span style='padding-right: 30px;'><strong><?php echo $this->data->member_id ; ?></strong></span>
									<?php endif ; 
										if(!empty($this->data->l_name) || !empty($this->data->f_name)):
									?>
									<span style='width:150px; padding-right: 30px;'>Member Name</span><span><strong><?php echo $this->data->l_name.' '.$this->data->f_name ; ?></strong></span>
								<?php endif; ?>
							</td>
						</tr>
						<tr>
							<td>
								<?php
									echo '<span style="padding-right: 10px;">From</span><br>' ; 
									echo $this->lists['yfrom'] ;
									echo $this->lists['mfrom'] ;
									echo $this->lists['dfrom'] ;
									echo '<br><span style="padding: 0 10px;">To</span><br>' ; 
									echo $this->lists['yto'] ;
									echo $this->lists['mto'] ;
									echo $this->lists['dto'] ;
								?>
								<br><br>
								<input type="button" name='fil_redemp_go' id='fil_redemp_go' value='Go' />
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
		
		<table>
			<tr>
				<?php if(!empty($this->total_points_used) || !empty($this->p_o_u)):?>
				<td valign='top' width='230px'>
					<table>
						<tr>
							<td>Total Points Earn</td>
							<td align="right"><?php echo empty($this->p_o_u_e)?'':number_format($this->p_o_u_e,0,'.',',');?></td>
						</tr>
						<tr>
							<td>Total Points Used</td>
							<td align="right"><?php 
								$pUsed = $this->p_o_u_e - $this->p_o_u->point;
								echo empty($pUsed)?'':number_format($pUsed,0,'.',',');
							?></td>
						</tr>
						<tr>
							<td>Points Balance</td>
							<td align="right">
							<?php 
								//echo $this->p_o_u;
								echo empty($this->p_o_u->point)?'':number_format($this->p_o_u->point, 0,'.',',');
							?>
							</td>
						</tr>
					</table>
				</td>
				<?php endif ; ?>
			 	<td style="text-align: right ;  width: 100px;" valign='top'>
			 		<?php if($this->user->authorise('wrd.profile.redemption.add', 'com_wrd')): ?>
	                <a href="index.php?option=com_wrd&controller=profile&layout=addredemption&id=<?php echo intval($_GET['id']); ?><?php if(!empty($this->data->user_id)){ echo '&uid='.$this->data->user_id ; } ?>">
	                     <img title="Add New" style="float: right ;" src="<?php echo JURI::base() .'/components/com_wrd/assets/images/add_new.png'?>">
	                 </a>
	                 <?php endif; ?>
			 	</td>
	 		</tr>
	 	</table>
    </div>
    
 	<div id='redemptions'>
		<table class="adminlist extra-adminlist table table-striped">
            <thead>
			 <tr>
				<th>Confirm Code</th>		
			 	<th>Product</th>
			 	<th>Exchange Date</th>
			 	<th>Points</th>
			 	<th>Cash</th>
			 	<th>Quality</th>		
			 	<th>Status</th>			
			 	<th>Approved Date</th>
				<th>Total Points</th>
				<th>Created By</th>
				<th>Updated By</th>
			 	<th>Actions</th>				
			 </tr>
             </thead>
             <tbody>
         <?php 
            /******** Get Redemption Ids ********/
            if(!empty($this->reedem)):
            {
                $rIds = array();
                $rId = '';
                $previousCode = '';
                $index = '';
				
                foreach ($this->reedem as $key => $item):
                
                    if($previousCode == ''):    
                        $previousCode = $item->code;
                    endif;
                    
                    if($item->code != $previousCode){
                        if(count($rIds) <= 0):
                            $rIds[0] = trim($rId, ',');
                        else:
                            $rIds[$index] = trim($rId, ',');
                        endif;
                        
                        $rId = '';
                        $index = '';
                   }
				  
                    if($index == ''):
                        $index = $key;
                    endif;
                    $rId .= $item->id.',';    
                    $previousCode = $item->code;
                
                endforeach;
                $rIds[$index] = trim($rId, ',');
				
            }
            endif;
			$point = 0;
         ?>    
		
		 <?php if(count($this->reedem) > 0): $previousCode = ''; $bg = 1; $index = 0; ?>
			
			<?php  foreach ($this->reedem as $key => $item): ?>
                <?php 
                   
					if($item->code != $previousCode):
                        $bg = ($bg == 0 ? 1 : 0);
                    endif;
					
                ?>
			 	<tr class="row<?php echo $bg; ?>">
			 		<!--<td><?php echo $item->id; ?></td>-->
					<td>
					<?php if($item->approved != 9): ?>
					<a title="Click here to update" href="index.php?option=com_wrd&controller=profile&layout=updateredemption&id=<?php echo $this->data->id; ?>&uid=<?php echo $this->data->user_id; ?>&code=<?php echo $item->code ;?>">
                    <?php 
                        echo ($item->code != $previousCode ? $item->code : '&nbsp;');
                    ?></a>
					<?php 
						else :
							echo ($item->code != $previousCode ? $item->code : '&nbsp;');
					endif;
					?>
					
					</td>				
				 	<!--<td><?php //echo $this->p_details[$item->product_id]->p_code ; ?></td> -->
				 	<td>
				 	<?php 
				 		$product_name = unserialize($this->p_details[$item->product_id]->name);
				 		echo $product_name['en-GB']; 
				 	?>
				 	</td>
				 	<td style="text-align: center;"><?php echo $item->exchange_date; ?></td>
				 	<td style="text-align: right;"><?php echo $item->point ; ?></td>
				 	<td style="text-align: right;"><?php echo $item->cash ; ?></td>
				 	<td  style="text-align: right;"><?php echo $item->quantity ; ?></td>		
				 	<td style="text-align: center;">
					 	<?php 
                            if($item->approved == 1) : 
                                echo 'Approve';
                            elseif($item->approved == 0) :
                                echo 'Waiting';
                            elseif($item->approved == 2) :
                                echo 'UnApproved';
							elseif($item->approved == 9) :
                                echo 'Deleted by us';	
                            endif;
                        ?>
				 	</td>			
				 	<td style="text-align: center;">
				 	<?php 
					 	if($item->approved_date != '0000-00-00 00:00:00') {
					 		echo $item->approved_date ;
					 	}	
				 	?>&nbsp;</td>
					<td><?php 
						
						if($item->code != $previousCode){
							
							if($item->approved != 9) {
							
								$totalPoint = WrdHelper::getTotalPointOfRedemption($item->code);
								echo number_format($totalPoint->total,0,".",",");
							}
						}
					  
					?></td>
					<td><?php echo $item->created_by ; ?></td>
					<td><?php echo $item->updated_by ; ?></td>
				 	<td  style="text-align: center;">
				 		<?php 
                            /****** Add links ******/
							if($item->code != $previousCode):   
								if($this->user->authorise('wrd.profile.redemption.cancel', 'com_wrd')):                                
									if($item->approved == 0) {
										echo '<a title="Approve for product" href="index.php?option=com_wrd&controller=profile&task=approve&id='.$this->data->id.'&code='.$item->code.'"><img src="components/com_wrd/assets/images/approve.png"></a>&nbsp;&nbsp;';
									}
									if($item->approved != 9) {
										echo '<a onclick="return check()" title="Click here to delete" href="index.php?option=com_wrd&controller=profile&task=r_approve&id='.$this->data->id.'&code='.$item->code.'"><img src="components/com_wrd/assets/images/not_approve.png"></a>';
									}
									echo '<a title="Click here to print" href="'.JURI::root().'administrator/index.php?option=com_wrd&controller=profile&layout=printredemption&code='.$item->code.'"><img style="float: right;" src="'.JURI::root().'images/print.jpg" /></a>';
								
								endif;
							
							endif; 
			 			?>
						<script>
							function check(){
								return confirm("Are You Sure ?");
							}
						</script>
				 	</td>
			 	</tr>
                <?php 
                    $previousCode = $item->code;
                ?>
			 <?php endforeach; ?>
		 <?php endif; ?>
            </tbody>
		 </table>
	 </div>
</div>